// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed


// 存储注释信息的数组
let annotations = [];

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "q" is now active!');

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with  registerCommand
	// The commandId parameter must match the command field in package.json
	const disposable = vscode.commands.registerCommand('q.helloWorld', function () {
		// The code you place here will be executed every time your command is executed

		// Display a message box to the user
		vscode.window.showInformationMessage('Hello World from !');
	});

	// 注册添加注释的命令
    let addAnnotationDisposable = vscode.commands.registerCommand('extension.addAnnotation', function () {
        const editor = vscode.window.activeTextEditor;
        if (editor) {
            const selection = editor.selection;
            // const selectedText = editor.document.getText(selection);
            const filePath = editor.document.fileName;
            const line = selection.start.line;
            // 提示用户输入注释内容
            vscode.window.showInputBox({ prompt: 'Enter a note for the selected code' }).then((note) => {
                if (note) {
                    annotations.push({ filePath, line, note });
                    // 更新侧边栏列表
                    updateAnnotationList();
                }
            });
        }
    });
    // 注册显示注释列表的命令
    let showAnnotationListDisposable = vscode.commands.registerCommand('extension.showAnnotationList', function () {
        updateAnnotationList();
    });


	// 更新侧边栏的注释列表
	function updateAnnotationList() {
		const items = annotations.map((annotation) => {
			return {
				label: `${annotation.note} (${annotation.filePath}:${annotation.line + 1})`,
				command: 'extension.openAnnotation',
				arguments: [annotation],
                value: annotation
			};
		});
		vscode.window.showQuickPick(items, { placeHolder: '请选择一个备注快速跳转~~' }).then((annotation) => {
            console.log('Opening annotation:', annotation.value);
            annotation = annotation.value;
            vscode.workspace.openTextDocument(annotation.filePath).then((document) => {
                vscode.window.showTextDocument(document).then((editor) => {
                    const lineCount = document.lineCount;
                    if (annotation.line >= 0 && annotation.line < lineCount) {
                        const position = new vscode.Position(annotation.line, 0);
                        const range = new vscode.Range(position, position);
                        editor.selection = new vscode.Selection(position, position);
                        editor.revealRange(range);
                    } else {
                        console.error('Invalid line number:', annotation.line);
                    }
                });
            }).catch((error) => {
                console.error('Error opening document:', error);
            });
        });
	}




	context.subscriptions.push(disposable,addAnnotationDisposable,showAnnotationListDisposable);



	
}

// This method is called when your extension is deactivated
function deactivate() {}

module.exports = {
	activate,
	deactivate
}
